package com.century.sjt.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.century.sjt.R;
import com.century.sjt.util.Constant;
import com.century.sjt.util.FileUtil;
import com.century.sjt.util.QRBitmapUtil;
import com.century.sjt.widget.TitleBar.SjtTitleBar;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;

/**
 * @author:周玲波
 * @Description: 二维码名片
 * @date : 2016/8/16 19:59
 */
public class MyQRRecordActivity extends BaseActivity{
    private SjtTitleBar titleBar;
    private ImageView imgQR;
    protected int mScreenWidth ;
    private ImageView userImg;
    private TextView userName;
    private RequestQueue mQueue;
    private TextView userId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qrcode_card);
        mQueue = Volley.newRequestQueue(MyQRRecordActivity.this);
        initView();
    }

    @Override
    protected void initView() {
        titleBar = (SjtTitleBar) findViewById(R.id.qr_titlebar);
        //       标题栏
        titleBar.setLeftIconResource(getResources().getString(R.string.icon_return));
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        titleBar.setTitle(getResources().getString(R.string.tab_my_qr));
        imgQR= (ImageView) findViewById(R.id.mqrrimage);
        userImg = (ImageView) findViewById(R.id.qrc_userimg);
        userName = (TextView) findViewById(R.id.qrc_username);
        SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
        userName.setText(mainSharedPreferences.getString("nickName", "用户"));
        userId = (TextView) findViewById(R.id.qrc_id);
        userId.setText(mainSharedPreferences.getString("mobile",""));
        final String userHeaderImg = mainSharedPreferences.getString("image","");
        if(!userHeaderImg.equals("")){
            Glide.with(MyQRRecordActivity.this).load(Constant.ImageHost_Big + userHeaderImg).into(userImg);

//            FileUtil.setupBigImage(userHeaderImg, mQueue, userImg);
        }
        Create2QR();


    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_my_qrrecord, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void Create2QR(){

        String uri = "addfriend:"+getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE).getString("userId", "");
//		Bitmap bitmap = BitmapUtil.create2DCoderBitmap(uri, mScreenWidth/2, mScreenWidth/2);
        Bitmap bitmap;
        try {
//            bitmap = QRBitmapUtil.createQRCode(uri, mScreenWidth);
            bitmap = Create2DCode(uri);
            if(bitmap != null){
                imgQR.setImageBitmap(bitmap);
//                imgQR.startAnimation(AnimationUtils.loadAnimation(MyQRRecordActivity.this,R.anim.navigation));   效果不明显，所以取消了
            }

        } catch (WriterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }

    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {

    }

    public Bitmap Create2DCode(String str) throws WriterException {
        //生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
        BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 300, 300);
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        //二维矩阵转为一维像素数组,也就是一直横着排了
        int[] pixels = new int[width * height];
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if(matrix.get(x, y)){
                    pixels[y * width + x] = 0xff000000;
                }

            }
        }

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        //通过像素数组生成bitmap,具体参考api
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    }
}
